package com.lvhx.controller;

import com.alibaba.fastjson.JSON;
import com.lvhx.dao.OrderMapper;
import com.lvhx.pojo.Order;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;

@Api(tags = "测试Lua脚本")
@RestController
@RequestMapping("lua")
public class LuaController {

    @Resource
    private RedisTemplate redisTemplate;

    @ApiOperation("直接执行String类型脚本")
    @GetMapping("/test1")
    public String test1() {

        redisTemplate.opsForValue().set("myKey", "myValue");

        String luaScript = "return redis.call('GET', KEYS[1])";

        DefaultRedisScript<String> script = new DefaultRedisScript<>();
        script.setScriptText(luaScript);
        script.setResultType(String.class);

        String myKey = (String) redisTemplate.execute(
                script,
                Collections.singletonList("myKey")
        );
        return JSON.toJSONString(myKey);

    }
}
